<?php
declare (strict_types = 1);
namespace app\api\controller;

// require '/../vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Iai\V20200303\IaiClient;
use TencentCloud\Iai\V20200303\Models\CompareFaceRequest;

use think\Request;
use think\Exception;
use app\BaseController;

use think\facade\{Log, Db};
use think\exception\ValidateException;

use app\api\validate\{UsersValidate};
use app\api\model\{Users};

class FaceController extends BaseController
{
    /**
	 * 录入人脸
	 */
    public function user_face_add()
    {
        if(request()->isPost()){
            $data = input('post.');
            $data['face'] = request()->file('face');

            // 验证数据
            try{
                validate(UsersValidate::class)->scene('face_add')->check($data);
            }catch(ValidateException $e){
                $this->error_back($e->getError());
            }

            $userModel = new Users();

            $data['id'] = $data['user_id'];
            $data['face'] = fileUp($data['face'], 'face_user');
            $field = ['id', 'face'];
            $flag = $userModel->edit($data, $field);

            if($flag['code'] != 'ok'){
                $this->error_back($flag['msg']);
            }

            $this->success_back('ok');
        }
    }

    /** 人脸对比 */
    public function user_face_check(){
        $data = input('post.');
        $data['face'] = request()->file('face');

        // 验证数据
        try{
            validate(UsersValidate::class)->scene('face_add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $userModel = new Users();

        /** 查询用户信息 */
        $user_info = $userModel->getFind(['id' => $data['user_id']]);

        if($user_info['code'] != 'ok'){
            $this->error_back('用户不存在或已删除');
        }

        try{
            $cred = new Credential("AKIDPYWP2P6O1h9MSKtJ6ogTcaKq8KG2DmQs", "Ox73y42ta6J9qPmvxlMIrFd1B6k80e4L");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("iai.tencentcloudapi.com");
            
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new IaiClient($cred, "ap-beijing", $clientProfile);

            $req = new CompareFaceRequest();
            
            $params = array(
                "Action"  => "CompareFace",
                "Version" => "2020-03-03",
                "Region"  => "ap-beijing",
                "UrlA"    => config('app.url') . fileUp($data['face'], 'face_check'),
                "UrlB"    => config('app.url') . $user_info['data']['face']
            );

            $req->fromJsonString(json_encode($params));

            $resp = $client->CompareFace($req);

            /** 接收返回的数据并转数组 */
            $face_data = json_decode($resp->toJsonString(), true);

           $this->success_back('ok', $face_data);
        }catch(TencentCloudSDKException $e){
            $this->error_back($e);
        }
    }
}